package com.zmovie.admin.controller.user;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmovie.admin.constant.Constants;
import com.zmovie.admin.controller.vo.UserVO;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.common.utils.MD5Utils;
import com.zmovie.framework.domain.User;
import com.zmovie.framework.domain.UserRole;
import com.zmovie.framework.service.UserRoleService;
import com.zmovie.framework.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 用户信息管理模块
 * @since 2023-12-05 21:51
 */
@Controller
@RequestMapping("/user/info")
@Api(tags = "用户信息管理模块")
public class UserInfoController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @GetMapping
    @ApiOperation("用户信息页面")
    public String index(Model model){
        //用户分页
        Page<User> userPage = userService.pageUser(Integer.valueOf(Constants.PAGE_NO), Integer.valueOf(Constants.PAGE_SIZE), new User());
        model.addAttribute(Constants.USER_PAGE_KEY, convert(userPage));
        //用户角色
        List<UserRole> userRoles = userRoleService.listUserRole(new UserRole());
        model.addAttribute(Constants.USER_ROLES_KEY, userRoles);
        return "user/user_info";
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取用户信息")
    @ResponseBody
    public Object getUser(@PathVariable("id") @NotNull Integer id){
        return CommonReturnType.success(userService.getById(id));
    }

    @PostMapping
    @ApiOperation("添加用户")
    @ResponseBody
    public Object addUser(@Validated User user) throws BusinessException {
        if(user.getRoleId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //加密
        user.setPassword(MD5Utils.code(user.getPassword()));
        userService.save(user);
        return CommonReturnType.success("添加成功!");
    }

    @PutMapping
    @ApiOperation("修改用户")
    @ResponseBody
    public Object updateUser(@Validated User user) throws BusinessException {
        if(user.getId() == null || user.getRoleId() == null){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //加密
        user.setPassword(MD5Utils.code(user.getPassword()));
        userService.updateById(user);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    @ResponseBody
    public Object deleteUser(@PathVariable("id") @NotNull Integer id){
        userService.removeById(id);
        return CommonReturnType.success("删除成功!");
    }

    @DeleteMapping
    @ApiOperation("批量删除用户")
    @ResponseBody
    public Object deleteUserBatch(Integer[] id) throws BusinessException {
        if(ArrayUtil.isEmpty(id)){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        userService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功!");
    }

    @PostMapping("/search")
    @ApiOperation("搜索")
    public String search(User user, Integer pageNo, Integer pageSize,Model model){
        Page<User> userPage = userService.pageUser(pageNo, pageSize, user);
        model.addAttribute(Constants.USER_PAGE_KEY, convert(userPage));
        return "user/user_info :: userList";
    }

    private UserVO convert(User user){
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        //用户角色
        UserRole userRole = userRoleService.getById(user.getRoleId());
        userVO.setUserRole(userRole);
        return userVO;
    }
    private Page<UserVO> convert(Page<User> userPage){
        Page<UserVO> userVOPage = new Page<>();
        BeanUtils.copyProperties(userPage, userVOPage);
        List<UserVO> userVOList = userPage.getRecords().stream().map(this::convert).collect(Collectors.toList());
        userVOPage.setRecords(userVOList);
        return userVOPage;
    }
}

